package net.gooduo.spring_boot_template.config;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.Method;

@Configuration
class WebMvcConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
//            SaRouter.match("/admin/**").check(r -> StpUtil.checkLogin());
//            SaRouter.match("/api/**").check(r -> StpUserUtil.checkLogin());

            if (handler instanceof HandlerMethod) {
                Method method = ((HandlerMethod)handler).getMethod();
                if ((Boolean) SaStrategy.me.isAnnotationPresent.apply(method, SaCheckLogin.class)) {
                    SaCheckLogin saCheckLogin = method.getAnnotation(SaCheckLogin.class);
//                    if (saCheckLogin.type().equals(StpUserUtil.TYPE)) {
//                        StpUserUtil.checkLogin();
//                    } else {
                        StpUtil.checkLogin();
//                    }
                }
            }

        })).addPathPatterns("/**");
    }
}
